﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class TempStudentDepositService
    {
        readonly ITempStudentDepositEntity entity;
        readonly IAuditEntity AuditEntity;
        public TempStudentDepositService()
        {
            this.entity = new TempStudentDepositEntity();
            this.AuditEntity = new AuditEntity();
        }

        #region Main Method

        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public long Save(object _model, ref string mess)
        {
            try
            {
                if (Validate((CoreData.TempStudentDeposit)_model, ref mess))
                {
                    Audit audit = new Audit();
                    audit.ModifiedDate = DateTime.Now;
                    audit.UserID = ValueDefine.UserID;
                    audit.Module = "Học viên đặt cọc";
                    audit.Code = "TempStudentDeposit";
                    var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                    if (id == 0)
                    {
                        audit.Action = "Thêm mới";
                        audit.Description = Diff(new CoreData.TempStudentDeposit(), (CoreData.TempStudentDeposit)_model);
                        AuditEntity.Save(audit, Table.TempStudentDeposit.ToString());
                        return entity.Save((CoreData.TempStudentDeposit)_model, Table.TempStudentDeposit.ToString());
                    }
                    else
                    {
                        audit.Action = "Cập nhật";
                        audit.Description = Diff(GetByID(id), (CoreData.TempStudentDeposit)_model);
                        AuditEntity.Save(audit, Table.Audit.ToString());
                        return entity.Update((CoreData.TempStudentDeposit)_model, Table.TempStudentDeposit.ToString());
                    }
                }
                else
                { return -1; }
            }
            catch { return -1; }
        }

        /// <summary>
        /// Get by ID
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public CoreData.TempStudentDeposit GetByID(long _id)
        {
            try
            {
                return entity.GetByCusTomSQL(String.Format(SQLCommand.GetTempStudentDeposit, " TempStudentDeposit.ID=" + _id)).First();
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.TempStudentDeposit> GetList()
        {
            try
            {
                return entity.GetByCusTomSQL(String.Format(SQLCommand.GetTempStudentDeposit, "1=1")).ToList();
            }
            catch { return null; }

        }

        /// <summary>
        /// Get List with Linq
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.TempStudentDeposit> GetListByLINQ(Func<CoreData.TempStudentDeposit, Boolean> _where)
        {
            try
            {
                return entity.GetMany(_where, Table.TempStudentDeposit.ToString()).ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// Get One with Linq
        /// </summary>
        /// <returns></returns>
        public CoreData.TempStudentDeposit GetOneByLINQ(Func<CoreData.TempStudentDeposit, Boolean> _where)
        {
            try
            {
                return entity.Get(_where, Table.TempStudentDeposit.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List with search string
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.TempStudentDeposit> GetList(string _searchstring)
        {
            //search theo tieu chi nao do         
            try
            {
                return entity.GetByCusTomSQL(String.Format(SQLCommand.GetTempStudentDeposit, _searchstring)).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public bool Delete(object _model)
        {
            try
            {
                entity.Delete((CoreData.TempStudentDeposit)_model, Table.TempStudentDeposit.ToString());

                Audit audit = new Audit();
                audit.ModifiedDate = DateTime.Now;
                audit.UserID = ValueDefine.UserID;
                audit.Module = "Học viên đặt cọc";
                audit.Code = "TempStudentDeposit";
                audit.Action = "Xóa";
                audit.Description = " Xóa học viên đặt cọc: " + ((CoreData.TempStudentDeposit)_model).NameExt;
                AuditEntity.Save(audit, Table.Audit.ToString());

                return true;
            }
            catch { return false; }
        }


        #endregion



        #region Other Method

        public TempStudentDeposit GetByTempStudentIdAndBranch(long tempStudentId, long branchId)
        {
            try
            {
                return entity.GetByCusTomSQL(String.Format(SQLCommand.GetTempStudentDeposit, " TempStudentDeposit.TempStudentID=" + tempStudentId + " and TempStudentDeposit.BranchID="+branchId)).FirstOrDefault();
            }
            catch { return null; }
        }

        bool Validate(CoreData.TempStudentDeposit obj, ref string mess)
        {
            if (obj.BranchID == 0)
            {
                new ValidateRequired("Ngành", "Branch", "");
            }
            if (obj.Money == 0)
            {
              //  new ValidateRequired("Tiền cọc", "Money", "");
            }
            
            mess = BusinessObject.Validate();
            BusinessObject.Clear();
            return mess == null ? true : false;
        }


        string Diff(CoreData.TempStudentDeposit OldItem, CoreData.TempStudentDeposit NewItem)
        {
            string content = "";
            if (NewItem.ID == 0)
            {
                content = "Thêm mới học viên đặt cọc: " + NewItem.NameExt;
            }
            else
            {
                if (OldItem.Money != NewItem.Money)
                {
                    content += "- Tiền cọc: " + OldItem.Money + " --> " + NewItem.Money + "\r\n";
                }
                if (OldItem.Note != NewItem.Note)
                {
                    content += "- Ghi chú: " + OldItem.Note + " --> " + NewItem.Note + "\r\n";
                }
                if (OldItem.DepositDate != NewItem.DepositDate)
                {
                    content += "- Ngày đặt cọc: " + OldItem.DepositDate + " --> " + NewItem.DepositDate + "\r\n";
                }
                if (OldItem.BranchExt != NewItem.BranchExt)
                {
                    content += "- Ngành đăng ký: " + OldItem.BranchExt + " --> " + NewItem.BranchExt + "\r\n";
                }
                
            }
            return content;
        }



        #endregion

    }

}
